{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import torch\n",
    "import torch.nn as nn\n",
    "import torch.functional as F"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "def main():\n",
    "    rnn = nn.RNN(input_size=100,hidden_size=20,num_layers=3)#layer指的是向上堆叠\n",
    "    x = torch.randn(10,3,100)#seq_len batch_size embedding_size\n",
    "    print(rnn)\n",
    "    out,h = rnn(x,torch.zeros(3,3,20))# num_layers batch_size embedding_size \n",
    "    #out是每个时刻的hidden输出 h是最后一个hidden的输出 \n",
    "    print(out.shape)\n",
    "    print(h.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "RNN(100, 20, num_layers=3)\n",
      "torch.Size([10, 3, 20])\n",
      "torch.Size([3, 3, 20])\n"
     ]
    }
   ],
   "source": [
    "main()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([3, 20])\n"
     ]
    }
   ],
   "source": [
    "xt = torch.randn(10,3,100)\n",
    "cell = nn.RNNCell(100,20)#embedding_size hidden_size\n",
    "h1 = torch.zeros(3,20)\n",
    "for x in xt:\n",
    "    #print(x.shape)torch.Size([3, 100])\n",
    "    h1 = cell(x,h1)\n",
    "print(h1.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "torch.Size([3, 30])\n",
      "torch.Size([3, 20])\n"
     ]
    }
   ],
   "source": [
    "cell1 = nn.RNNCell(100,30)#上下堆叠的情况\n",
    "cell2 = nn.RNNCell(30,20)\n",
    "h1 = torch.zeros(3,30)\n",
    "h2 = torch.zeros(3,20)\n",
    "for x in xt:\n",
    "    h1 = cell1(x,h1)#(3,100) (3,30) -> (3,30)\n",
    "    h2 = cell2(h1,h2)#(3,30) (3,20) -> (3,20)\n",
    "print(h1.shape)\n",
    "print(h2.shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "#LSTM\n",
    "cell = nn.LSTM(input_size=100,hidden_size=20,num_layers=4)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
